package ahblue.cn.tanzi.ui.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.kimkevin.cachepot.CachePot;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.circle.CircleDynamicItem;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.GlideApp;
import ahblue.cn.tanzi.util.TimeUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class CircleDynamicListActivity extends BaseActivity {

    @BindView(R.id.textViewTitle)
    TextView mTextViewTitle;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    private DynamicAdapter mDynamicAdapter;
    private List<CircleDynamicItem> mDynamicList = new ArrayList<>();

    private int mCircleId;

    private final static int ARG_PAGE_COUNT = 10;
    private int mPage = 0;

    private boolean mIsRefresh;
    private boolean mIsLoadMore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_dynamic_list);

        ButterKnife.bind(this);

        Intent intent = getIntent();
        mCircleId = intent.getIntExtra("CircleId", 0);

        initData();
        initView();
    }

    public void initData() {
        loadDynamic(mPage);
    }

    public void initView() {
        mTextViewTitle.setText("圈子动态");

        mDynamicAdapter = new DynamicAdapter(mDynamicList);
        mDynamicAdapter.setOnItemClickListener((adapter, view, position) -> {
            CircleDynamicItem item = mDynamicList.get(position);
            CachePot.getInstance().push(item);

            final Intent intent = new Intent(CircleDynamicListActivity.this, DynamicDetailActivity.class);
            CircleDynamicListActivity.this.startActivity(intent);
        });
        mRecyclerView.setAdapter(mDynamicAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mRefreshLayout.setOnRefreshListener(refreshlayout -> {
            mPage = 0;
            mDynamicList.clear();
            mIsRefresh = true;

            loadDynamic(mPage);
        });
        mRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
            mPage += 1;
            mIsLoadMore = true;

            loadDynamic(mPage);
        });
    }

    @OnClick(R.id.imageButtonLeft)
    void onClickButtonBack() {
        finish();
    }

    private void loadDynamic(int page) {
        RemoteRepository repo = RemoteRepository.getsInstance();
        Disposable d = repo.getBigShotCircleDetailsDynamic(mCircleId, page, ARG_PAGE_COUNT, mCallback);
        addRxDisposable(d);
    }

    private ApiCallback<List<CircleDynamicItem>> mCallback = new ApiCallback<List<CircleDynamicItem>>() {
        @Override
        public void onSuccess(List<CircleDynamicItem> data) {
            mDynamicList.addAll(data);
            mDynamicAdapter.notifyDataSetChanged();

            if (mIsLoadMore) {
                mIsLoadMore = false;
                mRefreshLayout.finishLoadmore(true);
            }

            if (mIsRefresh) {
                mIsRefresh = false;
                mRefreshLayout.finishRefresh(true);
            }
        }

        @Override
        public void onFailed(String errorMsg) {

            if (mIsLoadMore) {
                mIsLoadMore = false;
                mRefreshLayout.finishLoadmore(false);
            }

            if (mIsRefresh) {
                mIsRefresh = false;
                mRefreshLayout.finishRefresh(false);
            }
        }
    };

    class DynamicAdapter extends BaseQuickAdapter<CircleDynamicItem, BaseViewHolder> {

        DynamicAdapter(@Nullable List<CircleDynamicItem> data) {
            super(R.layout.item_circle_home_no_img, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CircleDynamicItem item) {
            final ImageView iv = helper.getView(R.id.iv_circle_home_img);
            GlideApp.with(iv).load(item.member_avatar)
                    .placeholder(R.drawable.user_default_avatar)
                    .error(R.drawable.user_default_avatar)
                    .into(iv);

            helper.setText(R.id.tv_circle_home_name, item.member_name);
            helper.setText(R.id.tv_circle_home_time, TimeUtils.getStandardDate(item.add_time));
            helper.setText(R.id.tv_circle_home_three_content, item.content);

            helper.setText(R.id.tv_dynamic_bottom_like, Integer.toString(item.fabulous_count));
            helper.setText(R.id.tv_dynamic_bottom_comment, Integer.toString(item.comment_count));
        }
    }
}
